package de.pierrepoenisch.checkyoursite.server;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import de.pierrepoenisch.checkyoursite.client.CheckResult;
import de.pierrepoenisch.checkyoursite.client.CheckResultStateET;
import de.pierrepoenisch.checkyoursite.client.CheckSiteService;

/**
 * this is a stub for the server to develope offline 
 * @author pierre
 *
 */
public class CheckSiteServiceStubImpl extends RemoteServiceServlet implements CheckSiteService{
	String siteContent = new String();
	String state = new String();
	
	private static Logger logger = Logger.getRootLogger(); 
	
//TODO move logging into an own file
//TODO add exception logging
static {
	//initialize logger
	PatternLayout layout = new PatternLayout("%d - %r [%t] %-5p %c %x - %m\n");
	ConsoleAppender consoleAppender = new ConsoleAppender(layout);
	logger.addAppender(consoleAppender);
	try {
		FileAppender fileAppender = new FileAppender(layout, 
				"CheckYourSite_Operating.log", false);
		logger.addAppender(fileAppender);
	} catch (IOException e) {
		e.printStackTrace();
	}
	//		 ALL | DEBUG | INFO | WARN | ERROR | FATAL | OFF:
	logger.setLevel(Level.DEBUG);
	logger.info("Logger initialized");
}
	
	
	/**
	 * method that checkes a site with all it's linked sites
	 */
	public CheckResult checkSite(String uri){
		logger.info("Checking Site:\"" + uri+"\"");
		logger.warn("WORKING WITH STUB ! This is only a help for implementation.");		
		
		CheckResult result = new CheckResult();				

		//set stub results
		result.setURI("www.stub.result");
		result.setSiteSizeKB(120.123);
		result.setState(CheckResultStateET.OK);
		result.setTimeInSec(3.23);
		
		//add another file to the results
		CheckResult result2 = new CheckResult();
		result.setReferredFilesCheckResult(new CheckResult[] {result2});
		result2.setURI("www.stub.result2");
		result2.setSiteSizeKB(32.23);
		result2.setState(CheckResultStateET.NOK);
		result2.setTimeInSec(0.12);
		
		//return the result
		return result;
	}
}
